<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>监听器的使用</title>
</head>
<body>
<!--
  监听器用来监听数据的变化
  数据一旦发生变化，就会触发监听器所绑定的方法。
  一般用来处理异步或者开销较大的操作
-->

<div id="app">
  <label> 名：
    <input type="text" v-model="firstName">
  </label>
  <br>
  <label> 姓：
    <input type="text" v-model="lastName">
  </label>
  <br>
  {{fullName}}
</div>

<script src="../lib/vue.js"></script>
<script>
  let vm = new Vue({
    el: '#app',
    data: {
      firstName: 'Jim',
      lastName: 'Green',
      fullName: 'Jim Green'
    },
    watch: {
      firstName(val) {
        console.log('firstName has changed')
        this.fullName = val + ' ' + this.lastName
      },
      lastName(val) {
        console.log('lastName has changed')
        this.fullName = this.firstName + ' ' + val
      }
    }
  })
</script>

</body>
</html>